#include "stdafx.h"
#include "web_page_mgr.h"
#include <commondef/glb_define.h>
#include <utility/ini_op.h>
#include "dll_object.h"
#include <utility/api_tools.h>
#include <interface/aot_im_define.h>

web_page_mgr::web_page_mgr()
{
	
}

web_page_mgr::~web_page_mgr()
{
	url_open_counts_.clear();
}

std::string
web_page_mgr::read_url(const char* key)
{
	aot::tt::utility::ini_op ini;
	std::string path =g_dll_obj->im_->get_config_path();
	path += "web_srv.ini";

	int isexist = ::GetFileAttributes(path.c_str());
	if( isexist == INVALID_FILE_ATTRIBUTES )
	{
		return NULL;
	}
	ini.file_name(path.c_str());
	std::string url;
	url = ini.read("URL", key, "");
	//ShellExecute(NULL, "open", url.c_str(), NULL, NULL, SW_SHOWNORMAL); 
	return url.c_str();
}

std::string 
web_page_mgr::read_oa_url()
{
	aot::tt::utility::ini_op ini;
	std::string path =g_dll_obj->im_->get_curr_user_path();
	path += "web_srv.ini";
	ini.file_name(path.c_str());
	std::string url;
	url = ini.read("URL", "tata_oa", "");
	if( !url.empty() )
	{
		url += "?token=";
		url += g_dll_obj->user_mgr_.token().c_str();
	}
	else
	{
		path = g_dll_obj->im_->get_config_path();
		path += "web_srv.ini";
		ini.file_name(path.c_str());
		url = ini.read("URL", "tata_oa", "");
	}

	return url;
}

int
web_page_mgr::open_page(int event_type)
{
	CString curr_url, title;
	std::string url;
	std::string token = g_dll_obj->user_mgr_.token();
	switch( event_type )
	{
	case aot::e_web_oa:
		{
			curr_url = read_oa_url().c_str();
			if( curr_url == "" )
			{
				return -2;
			}
			title = get_res_string(WEB_OA);
			open_page(curr_url, title, 502, 450, e_webpage_nocover);
			break;
		}
	case aot::e_web_create_tribe:
		{
			url = read_url("create_tribe");
			curr_url.Format("%s?token=%s", url.c_str(), token.c_str());
			title = get_res_string(WEB_CREATE_TRIBE);
			open_page(curr_url, title, 450, 394, e_webpage_cover);
			break;
		}
	case aot::e_web_modify_password:
		{
			url = read_url("modify_password");
			curr_url.Format("%s?token=%s", url.c_str(), token.c_str());
			title = get_res_string(WEB_MODIFY_PASSWORD);
			open_page(curr_url, title, 401, 345, e_webpage_cover);
			break;
		}
	case  aot::e_web_find_linkman:
		{
			url = read_url("search_buddy_tribe");
			curr_url.Format("%s?token=%s&flag=1", url.c_str(), token.c_str());
			title = get_res_string(WEB_FIND_LINKMAN);
			open_page(curr_url, title, 400, 300, e_webpage_cover);
			break;
		}
	case aot::e_web_find_tribe:
		{
			url = read_url("search_buddy_tribe");
			curr_url.Format("%s?token=%s&flag=2", url.c_str(), token.c_str());
			title = get_res_string(WEB_FIND_TRIBE);
			open_page(curr_url, title, 400, 300, e_webpage_cover);
			break;
		}
	case aot::e_web_find_bug_feedback:
		{
			url = read_url("bug_feedback");
			curr_url.Format("%s?token=%s", url.c_str(), token.c_str());
			title = get_res_string(WEB_FIND_BUG_FEED);
			open_page(curr_url, title, 600, 390, e_webpage_cover);
			break;
		}
	case aot::e_web_bind_mobile:
		{
			url = read_url("login_bind");
			if( url == "" )
			{
				return -2;
			}
			curr_url.Format("%s?token=%s", url.c_str(), token.c_str());
			title = get_res_string(WEB_BIND_MOBILE);
			open_page(curr_url, title, 450, 450, e_webpage_nocover, TRUE, event_type);
			break;
		}
	case aot::e_web_invite_buddy:
		{
			url = read_url("invite_buddy");
			if( url == "" )
			{
				return -2;
			}
			curr_url.Format("%s?token=%s", url.c_str(), token.c_str());
			title = get_res_string(WEB_INVITE_BUDDY);
			open_page(curr_url, title, 500, 550, e_webpage_nocover, TRUE, event_type);
			break;
		}
	case aot::e_web_call_cdr_query:
		{
			url = read_url("call_cdr_query");
			if( url == "" )
			{
				return -2;
			}
			curr_url.Format("%s?token=%s", url.c_str(), token.c_str());
			title = get_res_string(WEB_CALL_CDR_QUERY);
			open_page(curr_url, title, 450, 315,e_webpage_cover, TRUE,event_type);
			break;
		}
	default:
		return -1;
	}
	
	return 0;
}

int web_page_mgr::add_linkman_by_userid(LPCTSTR userid)
{
	CString curr_url, title;
	std::string url;
	std::string token = g_dll_obj->user_mgr_.token();
	url = read_url("search_buddy_tribe");
	curr_url.Format("%s?token=%s&flag=1&buddy_nbr=%s", url.c_str(), token.c_str(), userid);
	title = get_res_string(WEB_FIND_LINKMAN);
	open_page(curr_url, title, 400, 300, e_webpage_cover);
	return 0;
}

int web_page_mgr::open_page(const char* url, const char* title, int width, int height, int type, BOOL is_onlyone,int page_type)
{
	if ( is_onlyone )
	{
		iter_url_open_counts iter = url_open_counts_.find(url);
		if ( iter != url_open_counts_.end() )
		{
			web_dlg_info *p = iter->second;
			map_cwnd* pv = p->mwnd;
			map_cwnd::iterator pos = pv->begin();
			if( pos != pv->end() )
			{
				CWnd *pw = pos->second;
				if( pw->GetSafeHwnd() )
				{
					pw->ShowWindow(SW_SHOWNORMAL);
				}
			}
			if( p->mwnd->size() >= 1 )
			{
				return -2;
			}
		}
	}
	web_wrapper *web_page = new web_wrapper;
	web_page->set_info(title,TRUE,page_type);
	web_page->m_url_ = url;
	web_page->type_ = type;
	web_page->Create(web_wrapper::IDD, CWnd::GetDesktopWindow());
	
	int xscreen = GetSystemMetrics(SM_CXSCREEN);
	int yscreen = GetSystemMetrics(SM_CYSCREEN);

	CRect r;
	r.left = (xscreen - width) / 2;
	r.top = (yscreen - height) / 2;
	r.bottom = r.top + height;
	r.right = r.left + width;
	web_page->MoveWindow(r, TRUE);
	::SetForegroundWindow(web_page->GetSafeHwnd()); 
	web_page->ShowWindow(SW_SHOWNORMAL);
	
	iter_url_open_counts iter = url_open_counts_.find(url);
	
	if ( iter != url_open_counts_.end() )
	{
		web_dlg_info *p = iter->second;
		map_cwnd* pv = p->mwnd;
		size_t size = pv->size();
		web_page->index_ = size;
		pv->insert(map_cwnd::value_type(size, web_page));
		p->total_count++;
	}
	else
	{
		web_page->index_ = 0;
		web_dlg_info *info = new web_dlg_info;
		map_cwnd *pvnew = new map_cwnd;
		pvnew->insert(map_cwnd::value_type(0, web_page));
		info->total_count = 1;
		info->mwnd = pvnew;
		url_open_counts_.insert(std::make_pair(url, info));
	}
	return 0;
}

int web_page_mgr::open_html(const char* filename, const char* title, BOOL is_onlyone)
{
 	if ( is_onlyone )
 	{
 		iter_url_open_counts iter = url_open_counts_.find(filename);
 		if ( iter != url_open_counts_.end() )
 		{
			web_dlg_info *p = iter->second;
			map_cwnd* pv = p->mwnd;
			map_cwnd::iterator pos = pv->begin();
			if( pos != pv->end() )
			{
				CWnd *pw = pos->second;
				if( pw->GetSafeHwnd() )
				{
					pw->ShowWindow(SW_SHOWNORMAL);
				}
			}
			if( p->mwnd->size() >= 1 )
			{
				return -2;
			}
 		}
 	}
 	web_wrapper *web_page = new web_wrapper;
 	web_page->set_info(title, FALSE);
 	web_page->html_filename_ = filename;
 	web_page->Create(web_wrapper::IDD, CWnd::GetDesktopWindow());
 
 	int xscreen = GetSystemMetrics(SM_CXSCREEN);
 	int yscreen = GetSystemMetrics(SM_CYSCREEN);
 	int nWidth = 600;
 	int nHeight = 430;
 	CRect r;
 	r.left = (xscreen - nWidth) / 2;
 	r.top = (yscreen - nHeight) / 2;
 	r.bottom = r.top + nHeight;
 	r.right = r.left + nWidth;
 	web_page->MoveWindow(r, TRUE);
 	::SetForegroundWindow(web_page->GetSafeHwnd()); 
 	web_page->ShowWindow(SW_SHOWNORMAL);
 
 	iter_url_open_counts iter = url_open_counts_.find(filename);
 	if ( iter != url_open_counts_.end() )
 	{
		web_dlg_info *p = iter->second;
		map_cwnd* pv = p->mwnd;
		size_t size = pv->size();
		web_page->index_ = size;
		pv->insert(map_cwnd::value_type(size, web_page));
		p->total_count++;
 	}
 	else
 	{
		web_page->index_ = 0;
		web_dlg_info *info = new web_dlg_info;
		map_cwnd *pvnew = new map_cwnd;
		pvnew->insert(map_cwnd::value_type(0, web_page));
		info->total_count = 1;
		info->mwnd = pvnew;
 		url_open_counts_.insert(std::make_pair(filename, info));
 	}
	return 0;
}

int web_page_mgr::release_page(const char* url, int index)
{
 	iter_url_open_counts iter = url_open_counts_.find(url);
 	if ( iter != url_open_counts_.end() )
 	{
		map_cwnd::iterator iter1 = iter->second->mwnd->find(index);
		if( iter1 != iter->second->mwnd->end() )
		{
			if( iter1->second )
			{
				delete iter1->second;
			}
			iter->second->mwnd->erase(iter1);
		}
		if( iter->second->mwnd->size() == 0 )
		{
			if( iter->second->mwnd )
			{
				delete iter->second->mwnd;
				iter->second->mwnd = NULL;
			}
			delete iter->second;
			this->url_open_counts_.erase(iter);
		}
		return 0;
 	}
	return -1;
}

void
web_page_mgr::show_dlg()
{
	iter_url_open_counts iter = this->url_open_counts_.begin();
	for( ; iter != this->url_open_counts_.end(); iter++ )
	{
		map_cwnd::iterator iterhwnd = iter->second->mwnd->begin();
		for( ; iterhwnd != iter->second->mwnd->end(); iterhwnd++ )
		{
			if( (iterhwnd->second)->GetSafeHwnd() )
			{
				(iterhwnd->second)->ShowWindow(SW_SHOW);
			}
		}
	}
}

void
web_page_mgr::hide_dlg()
{
	iter_url_open_counts iter = this->url_open_counts_.begin();
	for( ; iter != this->url_open_counts_.end(); iter++ )
	{
		map_cwnd::iterator iterhwnd = iter->second->mwnd->begin();
		for( ; iterhwnd != iter->second->mwnd->end(); iterhwnd++ )
		{
			if( (iterhwnd->second)->GetSafeHwnd() )
			{
				(iterhwnd->second)->ShowWindow(SW_HIDE);
			}
		}
	}
}